<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="{__STATIC_PATH}layuiadmin/layui/css/layui.css" media="all">
    <link rel='stylesheet' href='{__STATIC_PATH}layuiadmin/style/admin.css' media='all'>
    <script src="{__STATIC_PATH}/layuiadmin/layui/layui.js"></script>
    <script>
        layui.config({
            base: '{__STATIC_PATH}layuiadmin/' //静态资源所在路径
        })
    </script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
            </div>
            <table id="table-list" lay-filter="lay-table-list"></table>
            <script type="text/html" id="table-content-list">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
            <script type="text/html" id="checkboxTpl">
                <!-- 这里的 checked 的状态只是演示 -->
                <input type="checkbox"  name="lock" value="{{d.id}}" title="关闭" lay-filter="lock" {{ d.is_lock == 2 ? 'checked' : '' }}>
            </script>
        </div>
    </div>
</div>
<script>
    layui.extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table','layer'], function(){
        var table = layui.table
            ,form = layui.form
            ,layer = layui.layer;
        var admin = layui.admin;
        var tableIns=table.render({
            elem: '#table-list' //指定原始表格元素选择器（推荐id选择器）
            ,url: '/admin/rbac/authGetListData' //数据接口
            ,height: 'full-100'//高度最大适应
            ,cols: [[
                {field:"icon",title:"图标",width:60},
                {field:"name",title:"菜单名称",minWidth:100},
                {field:"url",title:"url地址"},
                {field:"order_num",title:"权重"},
                {field:'status', title:'状态', width:110, templet: '#checkboxTpl', unresize: true},
                {title:"操作",minWidth:150,align:"center",fixed:"right",toolbar:"#table-content-list"}
            ]] //设置表头
            //,…… //更多参数参考右侧目录：基本参数选项
        });
        var $ = layui.$, active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加权限'
                    ,content: '/admin/rbac/authAdd'
                    ,maxmin: true
                    ,area: ['550px', '450px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            },
        };
        $('.layui-btn.layuiadmin-btn-list').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //监听工具条
        table.on('tool(lay-table-list)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定删除该数据？', function(index){
                    //obj.del();//删除对应行（tr）的DOM结构，并更新缓存
                    //向服务器发送删除请求
                    admin.req({
                        url: "/admin/rbac/authDel?id="+obj.data.id
                        ,type: "POST"
                        ,datatype:'json'
                        ,success: function(res){
                            if (res.code == 0) {
                                layer.msg('删除成功',{time:500},function(){
                                    tableIns.reload('table-list'); //重载表格
                                    parent.layer.close(index); //再执行关闭
                                });
                            }
                            else {
                                layer.msg(res.msg);
                            }
                        }
                    });
                    // $.ajax({
                    //     dataType:'json',
                    //     type: "POST",
                    //     url: "/admin/rbac/authDel?id="+obj.data.id,
                    //     success: function (Data) {
                    //         if (Data.code == 0) {
                    //             layer.msg('删除成功',{time:500},function(){
                    //                 tableIns.reload('table-list'); //重载表格
                    //                 parent.layer.close(index); //再执行关闭
                    //             });
                    //         }
                    //         else {
                    //             layer.msg(Data.msg);
                    //         }
                    //     },
                    //     error: function () {
                    //         alert("出现错误");
                    //         return false;
                    //     }
                    // }) //ajax结束
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑权限'
                    ,content: "/admin/rbac/authEdit?id="+ data.id
                    ,maxmin: true
                    ,area: ['550px', '450px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        });
        //监听锁定操作
        form.on('checkbox(lock)', function(obj){
            var is_lock=obj.elem.checked==true?2:1;
            //向服务器发送删除请求
            admin.req({
                url: "/admin/rbac/authLock?id="+obj.value+"&is_lock="+is_lock
                ,type: "POST"
                ,datatype:'json'
                ,success: function(res){
                    if (res.code == 0) {
                        if(is_lock==2){
                            layer.msg('关闭成功',{time:1000});
                        }else{
                            layer.msg('开启成功',{time:1000});
                        }
                    }
                    else {
                        layer.msg(res.msg);
                    }
                }
            });
            // $.ajax({
            //     dataType:'json',
            //     type: "GET",
            //     url: "/admin/rbac/authLock?id="+obj.value+"&is_lock="+is_lock,
            //     success: function (Data) {
            //         if (Data.code == 0) {
            //             if(is_lock==2){
            //                 layer.msg('关闭成功',{time:1000});
            //             }else{
            //                 layer.msg('开启成功',{time:1000});
            //             }
            //         }
            //         else {
            //             layer.msg(Data.msg);
            //         }
            //     },
            //     error: function () {
            //         alert("出现错误");
            //         return false;
            //     }
            // }) //ajax结束
        });
    });
</script>
</body>
</html>